<?php
$app = Blyn::app()->getActiveApp();
$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
$url = $screen->getLinkFromUrl(array('b_ajaxLinkId' => 'joinSpaceView' . "_" . "joinSpaceBtn"));
$spaces = $app->findSpaces();

$spaceNameStr = Yii::t('site', 'Space Name:');
$joinToSpaceStr = Yii::t('site', 'Join to Space');
$joinedAlreadyStr = Yii::t('site', 'Joined already');
?>
<script type="text/javascript">
    /*<![CDATA[*/
    jQuery(function($) {
        //alert("hello");
        jQuery('a[rel="tooltip"]').tooltip();
        jQuery('a[rel="popover"]').popover();
        jQuery('a[rel="popover"]').click(function() {
            //$(this).css({'margin':'0 auto 0 auto','top':'20%','left':'20%'});    
            // $(this).center();
        });
    });

    jQuery.fn.center = function() {
        this.css("position", "absolute");
        this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
                $(window).scrollTop()) + "px");
        this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
                $(window).scrollLeft()) + "px");
        return this;
    }
    /*]]>*/
    function request_join_Space(spaceId)
    {
        //alert(spaceId);
        var message = (document.getElementById("joinSpace_message" + spaceId).value);
        var applyingRole = $("input:radio[name ='applyingRole']:checked").val();
        //document.getElementById("joinSpace_message"+spaceId).focus();
        //alert("#joinbtn" + spaceId);        

        $.ajax({
            type: 'POST',
            url: '<?php echo $url; ?>',
            data: {'joined_spaceId': spaceId, 'join_Space_message': message, 'applyingRole': applyingRole},
            success: function(html) {
                $('#show_userSpaceList_content').html(html);
                $("#joinbtn" + spaceId).addClass('disabled btn-warning').text(<?php echo $joinedAlreadyStr; ?>);
                $("#joinbtn" + spaceId).popover('hide');
            }

        });

    }

</script>

<?php if (count($spaces) < 1) { ?>
    <div class="text-success well"> no space to find for join!</div> 
    <?php return;
} ?>

<table class="table table-stripe">
    <thead>
        <tr>
            <th>机构名称</th>
            <th>机构说明</th>
            <th>操作</th>
        </tr>
    </thead>
    <?php
    foreach ($spaces as $id => $space) {
        $spaceName = $space->getName();
        $spaceId = $space->getId();
        ?>
        <tr class="well well-small">
            <td><strong><?php echo $spaceNameStr; ?> <?php echo $spaceName; ?></strong></td>

            <td>Normal Space</td>
            <td><?php
                echo $control->getAjaxLink('加入机构', array(
                    'cooker' => 'BSpaceCooker@showJoinSpace',
                    'spaceId' => $spaceId,
                    'htmlOptions' => array(
                        'class' => '')));
                ?></td>
        </tr>
<?php } ?>
</table>




